﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using ApiBetaSeries.Model;
using ApiBetaSeries.Utils;

namespace ApiBetaSeries.Factories
{
    public class ShowFactory
    {
        /// <summary>
        /// Create a show from xml
        /// </summary>
        /// <param name="e">xml node</param>
        /// <returns>a show</returns>
        [ApiBetaSeries.Version.ApiMethod(true, "shows/display")]
        public static IShow CreateShow(XmlNode e)
        {
            IShow show = new Show();
            show.Title = UtilsXML.GetStringValue(e, "title");
            show.Id_thetvdb = UtilsXML.GetIntValue(e, "id_thetvdb");
            show.Url = UtilsXML.GetStringValue(e, "url");
            show.Description = UtilsXML.GetStringValue(e, "description");
            show.Status = UtilsEnum.StringToShowStatus(UtilsXML.GetStringValue(e, "status"));
            show.Banner = UtilsXML.GetStringValue(e, "banner");

            if (e["seasons"] != null)
            {
                show.Seasons = new List<int>();
                foreach (XmlElement s in e["seasons"].GetElementsByTagName("season"))
                {
                    // we assume seasons in order
                    show.Seasons.Add(UtilsXML.GetIntValue(s, "episodes"));
                }
            }

            if (e["genres"] != null)
            {
                show.Genres = new List<string>();
                foreach (XmlElement g in e["genres"].GetElementsByTagName("genre"))
                {
                    show.Genres.Add(g.FirstChild.Value);
                }
            }

            return show;
        }

        /// <summary>
        /// Create a show
        /// </summary>
        /// <returns>a show</returns>
        public static IShow CreateShow()
        {
            return new Show();
        }
    }
}
